﻿using GDGeek;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MrPP.SerriedCabinet
{//server
    public class BoxProcess : GDGeek.Singleton<BoxProcess>
    {
        public BoxModel _model;


        public Task open(CabinetGroup.ItemData data)
        {
            Debug.LogError(JsonUtility.ToJson(data));
            Task task = null;
            if (data.target != -1)
            {

                
               // string uri = Restful.RestfulManager.Uri("open");
                //var item = CabinetGroup.Instance.data.cabinets.item[data.target];
               
               // var op = Restful.RestfulManager.Instance.options(uri, new System.Collections.Generic.Dictionary<string, string> { { "area", item.area.ToString() }, { "column", item.index.ToString()} });
                task = BoxOperation.Instance.open(data.target);
                
                /*Restful.RestfulManager.Instance.getTask<ResultData>(op, delegate (ResultData result)
                {
                    Debug.Log("open:" + result.time);
                });*/

            }
            else
            {
                task = BoxOperation.Instance.close();
                /*
                string uri = Restful.RestfulManager.Uri("close");
                var op = Restful.RestfulManager.Instance.options(uri);
                task = Restful.RestfulManager.Instance.getTask<ResultData>(op, delegate (ResultData result)
                {
                    Debug.Log("close:" + result.time);
                });*/

            }
          
            return task;
        }

        public void post(string item1, object item2)
        {
          
            switch (item1)
            {
                case "box_left_page":
                    Debug.LogError("go:" + (int)(item2) + "lPage:" + _model.lPage);
                    _model.lPage = _model.lPage + (int)(item2);
                    break;
                case "box_right_page":
                    _model.rPage = _model.rPage + (int)(item2);
                    break;
                case "box_left_check":
                    checkLeft();
                    break;
                case "box_right_check":
                    checkRight();
                    break;
                case "box_box":
                    if (_model.box != (int)(item2)) { 
                        _model.lPage = 0;
                        _model.rPage = 0;
                        _model.box = (int)(item2);
                        _model.datas.Clear();
                        _model.clearLight();
                        refresh();
                    }
                    break;
                case "box_menu":
                    {
                        int index = (int)(item2);
                        if (index >= 0 && index < _model.datas.Count)
                        {
                            _model.menu = index;
                            _model.lit = _model.datas[index];
                            guide(_model.datas[index]);
                        }
                        else {
                            _model.clearLight();
                           
                        }

                    }

                    break;
            }
        }

        private void checkRight()
        {

            List<CabinetGroup.ItemData> right = new List<CabinetGroup.ItemData>();
            if (CheckReceiver.Instance) 
            {
                foreach (var data in _model.datas) {
                    if (data.type == Box.Type.Right) {
                        right.Add(data);
                    }
                }
            }

            CheckReceiver.Instance.loading(right.ToArray());
            Model.Instance.search.isOpen = 1;
        }

        private void checkLeft()
        {

            List<CabinetGroup.ItemData> left = new List<CabinetGroup.ItemData>();
            if (CheckReceiver.Instance)
            {
                foreach (var data in _model.datas)
                {
                    if (data.type == Box.Type.Left)
                    {
                        left.Add(data);
                    }
                }
            }

            CheckReceiver.Instance.loading(left.ToArray());
            Model.Instance.search.isOpen = 1;


        }

        private void guide(CabinetGroup.ItemData data)
        {
            TaskManager.Run(open(data));
        }
        private Task refreshTask(int box) {
            if (box < 0 || box > CabinetGroup.Instance.data.cabinets.item.Length) {
                return new Task();
            }
            var item = CabinetGroup.Instance.data.cabinets.item[box];
            var op = Restful.RestfulManager.Instance.options(Restful.RestfulManager.Uri("archives"), new Dictionary<string, string> { { "column", item.index.ToString() },{ "area", item.area.ToString() } });
            var task = Restful.RestfulManager.Instance.getArrayTask<ArchivesData>(op, delegate (ArchivesData[] datas) {
                if (_model.datas.Count != 0) {
                    _model.datas.Clear();
                }
                foreach (var data in datas) {
                   // Debug.LogError("!!!!"+ data.doc_name);
                    _model.datas.Add(CabinetGroup.Instance.getItem(data));
                }
            });
            return task;
        }
        private void refresh()
        {
            TaskManager.Run(refreshTask(_model.box));
        }
    }
}